package com.crm.miaohe.controller.openid;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.openid.request.UserOpenidQueryParams;
import com.crm.miaohe.model.TblUserOpenid;
import com.crm.miaohe.service.UserOpenidService;

import net.minidev.json.JSONObject;

@RestController
@RequestMapping("/api/erp/openid")
public class UserOpenidController {

	private final UserOpenidService userOpenidService;
	
	 public UserOpenidController(UserOpenidService userOpenidService) {
        this.userOpenidService = userOpenidService;
	 }


	 @GetMapping("/list")
	 public ResponseEntity<PageResponse<TblUserOpenid>> view(@Valid UserOpenidQueryParams params) {
		 return ResponseEntity.ok(userOpenidService.getOpenidDetails(params));
	 }
	 
	 @DeleteMapping("/delete/{id}")
	 public ResponseEntity<Void> deleteUserOpenid(@PathVariable String id) {
		 userOpenidService.deleteUserOpenid(id);
		 return ResponseEntity.ok().build();
	 }
	 
	 @GetMapping("/find")
	 public ResponseEntity<PageResponse<Boolean>> findById(@RequestParam String openid) {
		 boolean flag = false;
		 List<TblUserOpenid> list = userOpenidService.findUserOpenid(openid);
		 if (list.size() > 0) {
			 flag = true;
		 }
		 List<Boolean> boolList = new ArrayList<Boolean>();
		 boolList.add(flag);
		 return ResponseEntity.ok(PageResponse.<Boolean>builder().records(boolList).build());
	 }
}
